package com.pearadmin.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.hospital.domain.HospitalReturnsupplier;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.hospital.service.IHospitalReturnsupplierService;

import java.time.LocalDateTime;

/**
 * 退货给供应商Controller
 *
 * @author yuan
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/hospital/returnsupplier")
public class HospitalReturnsupplierController extends BaseController
{
    private String prefix = "hospital/returnsupplier";

    @Autowired
    private IHospitalReturnsupplierService hospitalReturnsupplierService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/main','hospital:returnsupplier:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询退货给供应商列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/data','hospital:returnsupplier:data')")
    public ResultTable list(@ModelAttribute HospitalReturnsupplier hospitalReturnsupplier, PageDomain pageDomain)
    {
        PageInfo<HospitalReturnsupplier> pageInfo = hospitalReturnsupplierService.selectHospitalReturnsupplierPage(hospitalReturnsupplier,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增退货给供应商
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/add','hospital:returnsupplier:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存退货给供应商
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/add','hospital:returnsupplier:add')")
    public Result save(@RequestBody HospitalReturnsupplier hospitalReturnsupplier)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalReturnsupplier.setCreateTime(LocalDateTime.now());
        hospitalReturnsupplier.setCreateBy(sysUser.getUserId());
        hospitalReturnsupplier.setCreateName(sysUser.getUsername());
        return decide(hospitalReturnsupplierService.insertHospitalReturnsupplier(hospitalReturnsupplier));
    }

    /**
     * 修改退货给供应商
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/edit','hospital:returnsupplier:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        HospitalReturnsupplier hospitalReturnsupplier = hospitalReturnsupplierService.selectHospitalReturnsupplierById(id);
        mmap.put("hospitalReturnsupplier", hospitalReturnsupplier);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存退货给供应商
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/edit','hospital:returnsupplier:edit')")
    public Result update(@RequestBody HospitalReturnsupplier hospitalReturnsupplier)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalReturnsupplier.setUpdateTime(LocalDateTime.now());
        hospitalReturnsupplier.setUpdateBy(sysUser.getUserId());
        hospitalReturnsupplier.setUpdateName(sysUser.getUsername());
        return decide(hospitalReturnsupplierService.updateHospitalReturnsupplier(hospitalReturnsupplier));
    }

    /**
     * 删除退货给供应商
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/remove','hospital:returnsupplier:remove')")
    public Result batchRemove(String ids)
    {
        return decide(hospitalReturnsupplierService.deleteHospitalReturnsupplierByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/hospital/returnsupplier/remove','hospital:returnsupplier:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(hospitalReturnsupplierService.deleteHospitalReturnsupplierById(id));
    }
}
